﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.DataObject;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess.BusinessObject
{
    public class BCustomer:Common.CDBase
    {
        public bool Add(OCustomer _OCustomer)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@OrganizationName",SqlDbType.NVarChar)
                                      , new SqlParameter("@CustomerName",SqlDbType.NVarChar)
                                      , new SqlParameter("@Address",SqlDbType.NVarChar)
                                      , new SqlParameter("@TaxNumber",SqlDbType.VarChar)
                                      , new SqlParameter("@Tel",SqlDbType.VarChar)
                                      , new SqlParameter("@Fax",SqlDbType.VarChar)
                                      , new SqlParameter("@MobilePhone",SqlDbType.VarChar)
                                      , new SqlParameter("@Email",SqlDbType.VarChar)
                                      , new SqlParameter("@ShipAddress",SqlDbType.NVarChar)
                                      , new SqlParameter("@CouponCode",SqlDbType.NVarChar)};
            sqlParam[0].Value = _OCustomer.OrganizationName;
            sqlParam[1].Value = _OCustomer.CustomerName;
            sqlParam[2].Value = _OCustomer.Address;
            sqlParam[3].Value = _OCustomer.TaxNumber;
            sqlParam[4].Value = _OCustomer.Tel;
            sqlParam[5].Value = _OCustomer.Fax;
            sqlParam[6].Value = _OCustomer.MobilePhone;
            sqlParam[7].Value = _OCustomer.Email;
            sqlParam[8].Value = _OCustomer.ShipAddress;
            sqlParam[9].Value = _OCustomer.CouponCode;
            if (RunProcedure("sp_tblCustomer_add", sqlParam))
            {
                return true;
            }
            return false;
        }
        public IList<OCustomer> Get(int CustomerID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@CustomerID", SqlDbType.Int) };
            sqlParam[0].Value = CustomerID;
            return Common.Common.ConvertTo<OCustomer>(RunProcedureGet("sp_tblCustomer_get", sqlParam));
        }
        public IList<OCustomer> Get(string Name, string Address, string TaxNumber, string Tel,string MobilePhone, string CouponCode, string Email, string Order, string OrderBy, int PageIndex, int PageSize)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@Name",SqlDbType.NVarChar)
                                      , new SqlParameter("@Address",SqlDbType.NVarChar)
                                      , new SqlParameter("@TaxNumber",SqlDbType.VarChar)
                                      , new SqlParameter("@Tel",SqlDbType.VarChar)
                                      , new SqlParameter("@MobilePhone",SqlDbType.VarChar)
                                      , new SqlParameter("@Email",SqlDbType.VarChar)
                                      , new SqlParameter("@CouponCode",SqlDbType.NVarChar)
                                      , new SqlParameter("@Order",SqlDbType.VarChar)
                                      , new SqlParameter("@OrderBy",SqlDbType.VarChar)
                                      , new SqlParameter("@PageIndex",SqlDbType.Int)
                                      , new SqlParameter("@PageSize",SqlDbType.Int)};
            sqlParam[0].Value = Name;
            sqlParam[1].Value = Address;
            sqlParam[2].Value = TaxNumber;
            sqlParam[3].Value = Tel;
            sqlParam[4].Value = MobilePhone;
            sqlParam[5].Value = Email;
            sqlParam[6].Value = CouponCode;
            sqlParam[7].Value = Order;
            sqlParam[8].Value = OrderBy;
            sqlParam[9].Value = PageIndex;
            sqlParam[10].Value = PageSize;
            return Common.Common.ConvertTo<OCustomer>(RunProcedureGet("sp_tblCustomer_get", sqlParam));
        }

        public bool Update(OCustomer _OCustomer)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@CustomerID",SqlDbType.Int)
                                      , new SqlParameter("@OrganizationName",SqlDbType.NVarChar)
                                      , new SqlParameter("@CustomerName",SqlDbType.NVarChar)
                                      , new SqlParameter("@Address",SqlDbType.NVarChar)
                                      , new SqlParameter("@TaxNumber",SqlDbType.VarChar)
                                      , new SqlParameter("@Tel",SqlDbType.VarChar)
                                      , new SqlParameter("@Fax",SqlDbType.VarChar)
                                      , new SqlParameter("@MobilePhone",SqlDbType.VarChar)
                                      , new SqlParameter("@Email",SqlDbType.VarChar)
                                      , new SqlParameter("@ShipAddress",SqlDbType.NVarChar)
                                      , new SqlParameter("@CouponCode",SqlDbType.NVarChar)};
            sqlParam[0].Value = _OCustomer.CustomerID;
            sqlParam[1].Value = _OCustomer.OrganizationName;
            sqlParam[2].Value = _OCustomer.CustomerName;
            sqlParam[3].Value = _OCustomer.Address;
            sqlParam[4].Value = _OCustomer.TaxNumber;
            sqlParam[5].Value = _OCustomer.Tel;
            sqlParam[6].Value = _OCustomer.Fax;
            sqlParam[7].Value = _OCustomer.MobilePhone;
            sqlParam[8].Value = _OCustomer.Email;
            sqlParam[9].Value = _OCustomer.ShipAddress;
            sqlParam[10].Value = _OCustomer.CouponCode;
            if (RunProcedure("sp_tblCustomer_update", sqlParam))
            {
                return true;
            }
            return false;
        }
        public int GetIDLastRecord()
        {
            DataTable dtb = RunProcedureGet("sp_tblCustomer_getidlastrecord");
            if (dtb.Rows.Count > 0)
            {
                return int.Parse(dtb.Rows[0]["CustomerID"].ToString());
            }
            return 0;
        }
        public bool Delete(int CustomerID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@CustomerID", SqlDbType.Int) };
            sqlParam[0].Value = CustomerID;
            return RunProcedure("sp_tblCustomer_delete", sqlParam);
        }
    }
}
